//
//  dEndViewController.m
//  Surveyer
//
//  Created by Qikun Guo on 13-8-18.
//  Copyright (c) 2013年 Tim. All rights reserved.
//

#import "dEndViewController.h"

@interface dEndViewController ()

@end

@implementation dEndViewController
@synthesize currentSurvey;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = NSLocalizedString(@"This is the end page", @"display in front");
    self.finishButton.titleLabel.text = NSLocalizedString(@"Finish", @"button text");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)completed:(id)sender
{
    NSString *s = [self testValidity];
    if (!s)
    {
        for (Result *r in currentSurvey.result)
        {
            r.completed = YES;
        }
        currentSurvey.time++;
        [self.delegate end];
    }
    else
    {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Not Finished!", @"show if some questions are not done in survey") message:[NSString stringWithFormat:@"%@%@%@",NSLocalizedString(@"Question ", nil),s,NSLocalizedString(@" is not finished!", nil)] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [av show];
    }
}

- (NSString *)testValidity
{
    
    for (Result *r in currentSurvey.result)
    {
        if (r.text.length == 0 || !r.text)
            return r.item.title;
    }
    return nil;
}
@end
